package com.spt.pay.outer;

import com.oaf.response.JsonMarshaller;
import com.oaf.response.XmlMarshaller;
import com.spt.exception.SptPayException;
import com.spt.utils.SptUtils;

import java.io.ByteArrayOutputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by arlen on 2017/1/14.
 */
public class MarshallerDemo {
    public static void main(String args[]){
        Map sParaTemp=new HashMap();
        MarshallerDemo.marshaller(sParaTemp, "json");
    }
    /**
     * object 转为报文....
     *
     * @param object
     * @param format
     * @return
     */
    public static String marshaller(Object object, String format) {
        JsonMarshaller jsonMarshaller = new JsonMarshaller();
        XmlMarshaller xmlMarshaller = new XmlMarshaller();

        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);

            if (format.toLowerCase().equals("json")) {
                if (object != null) {
                    jsonMarshaller.marshaller(object, bos);
                } else {
                    return "";
                }
            } else {
                if (object != null) {
                    xmlMarshaller.marshaller(object, bos);
                } else {
                    return "";
                }
            }

            return bos.toString();
        } catch (Exception e) {
            throw new SptPayException(e);
        }
    }
}
